home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1992-1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- #include "Timer.h"
-
- const int RUNNING = 1;
- const int STOPPED = 0;
-
- void WatchTime::set(SbTime time)
- {
- long sec, usec;
-
- time.getValue(sec, usec);
-
- _hours = sec/3600;
- _minutes = sec/60;
- _seconds = sec%60;
- _tenths = usec/100000;
- }
-
- WatchTime operator +(const WatchTime &t0, const WatchTime &t1)
- {
- WatchTime result(
- t0._hours + t1._hours,
- t0._minutes + t1._minutes,
- t0._seconds + t1._seconds,
- t0._tenths + t1._tenths);
-
- if (result._tenths >= 10)
- {
- result._tenths -= 10;
- result._seconds++;
- }
-
- if (result._seconds >= 60)
- {
- result._seconds -= 60;
- result._minutes++;
- }
-
- if (result._minutes >= 60)
- {
- result._minutes -= 60;
- result._hours++;
- }
-
- return result;
- }
-
-
-
- void Timer::reset()
- {
- _this_lap.reset();
- _last_lap.reset();
- _best_lap.reset(); // XXX should read from file
-
- _state = STOPPED;
- }
-
- void Timer::update()
- {
- if (_state == RUNNING)
- {
- SbTime now = SbTime::getTimeOfDay();
- WatchTime delta(now - _start);
- _this_lap = _offset + delta;
- }
- }
-
-
- void Timer::lap()
- {
- static Boolean best_lap_set = FALSE;
-
- if ((_this_lap < _best_lap) || (! best_lap_set))
- {
- _best_lap = _this_lap;
- best_lap_set = TRUE;
- }
-
- _last_lap = _this_lap;
- _this_lap.set(0,0,0,0);
-
- start();
- }
-
-
- void Timer::start()
- {
- _offset = _this_lap;
- _start = SbTime::getTimeOfDay();
- _state = RUNNING;
- }
-
-
- void Timer::stop()
- {
- _state = STOPPED;
- }
-
-
- // sets string to be in the form 0:00:00.0
- void WatchTime::setString(char buf[10]) const
- {
- sprintf(buf,"%1d:%2d:%2d.%1d",_hours,_minutes,_seconds,_tenths);
-
- // stick in leading zeros
- if (_minutes < 10)
- buf[2] = '0';
- if (_seconds < 10)
- buf[5] = '0';
-
- buf[9] = NULL;
- }
-
- Boolean Timer::get_this_lap(char *(&result)) const
- {
- static WatchTime last(-1,-1,-1,-1);
- static char buf[10]; // 0:00:00.0
- Boolean changed;
-
- if (changed = (last != _this_lap))
- {
- _this_lap.setString(buf);
- last = _this_lap;
- }
-
- result = &(buf[0]);
- return changed;
- }
-
- Boolean Timer::get_last_lap(char *(&result)) const
- {
- static WatchTime last(-1,-1,-1,-1);
- static char buf[10]; // 0:00:00.0
- Boolean changed;
-
- if (changed = (last != _last_lap))
- {
- _last_lap.setString(buf);
- last = _last_lap;
- }
-
- result = &(buf[0]);
- return changed;
- }
-
- Boolean Timer::get_best_lap(char *(&result)) const
- {
- static WatchTime last(-1,-1,-1,-1);
- static char buf[10]; // 0:00:00.0
- Boolean changed;
-
- if (changed = (last != _best_lap))
- {
- _best_lap.setString(buf);
- last = _best_lap;
- }
-
- result = &(buf[0]);
- return changed;
- }
-
-